package com.example.scandemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.ScanDevice;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    private ScanDevice mScanDevice;
    private MediaPlayer mmediaplayer;
    private EditText showScanResult;
    private EditText mtime;

    private BroadcastReceiver mScanDataReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if (action.equals("ACTION_BAR_SCAN")) {
                String str = intent.getStringExtra("EXTRA_SCAN_DATA");
                if(str != null)
                    mmediaplayer.start();

                showScanResult.append(str + "\n");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mScanDevice = new ScanDevice(this);  //初始化接口  Initialization interface

        IntentFilter scanDataIntentFilter = new IntentFilter();
        scanDataIntentFilter.addAction("ACTION_BAR_SCAN");
        registerReceiver(mScanDataReceiver, scanDataIntentFilter);

        mmediaplayer = new MediaPlayer(); //   Initialize sound
        mmediaplayer = MediaPlayer.create(this, R.raw.scanok);
        mmediaplayer.setLooping(false);

        showScanResult = (EditText) findViewById(R.id.editText1);
//        mtime = (EditText) findViewById(R.id.time);
        mScanDevice.openScan();  //Power on
    }

    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.openScanner:
//                mScanDevice.openScan();  //Power on
//                break;
//            case R.id.closeScanner:
//                mScanDevice.closeScan(); // Power off
//                break;
//            case R.id.startDecode:
//                mScanDevice.startScan(); // star scan
//                break;
//            case R.id.stopDecode:
//                mScanDevice.stopScan(); // stop scan
//                break;
//            case R.id.start_continue:
//                mScanDevice.ContinousStart();  //open continued scanner
//                break;
//            case R.id.stop_continue:
//                mScanDevice.ContinousStop();//close  continued scanner
//                break;
//            case R.id.close:
//                finish();
//                break;
//            case R.id.ceanl:
//                showScanResult.setText("");
//                break;
//            case R.id.scan_time:
////                int time = Integer.parseInt(mtime.getText().toString());
////                if(time > 30) {
////
////                    mScanDevice.TimeScan(time);
////                }
//
//            default:
//                break;
//        }
    }

    //  Monitor Scan Key
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_F4:
            case KeyEvent.KEYCODE_F7:
            case KeyEvent.KEYCODE_F1:
            case KeyEvent.KEYCODE_F2:
            case KeyEvent.KEYCODE_F8:
                // scanning
                if (event.getRepeatCount() == 0) {
                    mScanDevice.startScan();
                }
                return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();

        if (mScanDevice != null) {
            mScanDevice.closeScan();
            mScanDevice = null;
            unregisterReceiver(mScanDataReceiver);
        }
        if(mmediaplayer != null){
            mmediaplayer.release();
            mmediaplayer = null;
        }
    }

}